import {
  getList,
  getDetail,
  delList,
  caliOpenOrStop,
  addList,
  updateCali,
  finishPlan,
  getProgress,
} from 'service/actions/deviceCali';
import { message } from 'antd';
import router from 'umi/router';

export default {
  namespace: 'deviceCali',

  state: {
    list: [],
    detail: {},
    details: {},
    total: 0,
    pageCurrent: 1,
    allPages: 1,
    progressList: [],
    serachParams: {}
  },

  effects: {
    // 获取列表数据
    *getDataList({ payload }, { put, call }) {
      const data = yield call(getList, payload || {});
      yield put({
        type: 'updateList',
        payload: {
          list: data.data.records,
          total: data.data.total,
          pageCurrent: data.data.current,
          allPages: data.data.pages,
        },
      });
    },
    // 增加列表数据
    *addCaliList({ payload }, { put, call }) {
      const data = yield call(addList, payload);
      if (data.code === '000000') {
        message.success('操作成功！', 1.5, () => {
          router.push('/deviceCali');
        });
        yield put({
          type: 'updateList',
          payload: {
            list: data.data.records,
            total: data.data.total,
            pageCurrent: data.data.current,
            allPages: data.data.pages,
          },
        });
      } else {
        message.error(data.mesg);
      }
    },
    // 修改检定计划
    *updateCali({ payload }, { put, call }) {
      const data = yield call(updateCali, payload);
      if (data.code === '000000') {
        message.success('操作成功！', 1.5, () => {
          router.push('/deviceCali');
        });
      } else {
        message.error(data.mesg);
      }
      yield put({
        type: 'updateList',
        payload: {
          list: data.data.records,
          total: data.data.total,
          pageCurrent: data.data.current,
          allPages: data.data.pages,
        },
      });
    },
    // 完成计划
    *finishPlanFn({ payload }, { put, call }) {
      const data = yield call(finishPlan, payload);
      if (data.code === '000000') {
        message.success('操作成功！', 1.5, () => {
          router.push('/deviceCali');
        });
      } else if (data.code === '030200' || data.code === '-1') {
        message.error(data.mesg)
        yield put({ type: 'getDetail', payload: { id: payload.id } });
      } else {
        message.error(data.mesg);
      }
    },
    // 删除列表数据
    *delList({ payload }, { put, call }) {
      const data = yield call(delList, payload);
      if (data.code === '000000') {
        message.success(data.mesg);
        yield put({
          type: 'updateSerachParams',
          payload: {
            serachParams: {
              current: 1
            }
          }
        });
      } else {
        message.error(data.mesg);
        yield put({
          type: 'updateSerachParams',
          payload: {
            serachParams: {
              current: 1
            }
          }
        });
      }
    },
    // 设备停用启用
    *caliStopOrOpen({ payload }, { put, call }) {
      const data = yield call(caliOpenOrStop, payload);
      if (data.code === '000000') {
        message.success(data.mesg);
        yield put({
          type: 'updateSerachParams',
          payload: {
            serachParams: {
              current: 1
            }
          }
        });
      } else {
        message.error(data.mesg);
        yield put({
          type: 'updateSerachParams',
          payload: {
            serachParams: {
              current: 1
            }
          }
        });
      }
    },
    // 获取详情数据
    *getDetail({ payload }, { put, call }) {
      const res = yield call(getDetail, payload);
      if (res.code === '000000') {
        res.data.strLen = res.data.remark && res.data.remark.length || 0;
        yield put({
          type: 'updateDetail',
          payload: { detail: res.data },
        });
      }
      return res
    },
    // 获取详情页面的详情数据
    *getDetails({ payload }, { put, call }) {
      const res = yield call(getDetail, payload);
      if (res.code === '000000') {
        res.data.strLen = res.data.remark && res.data.remark.length || 0;
        yield put({
          type: 'updateDetails',
          payload: { details: res.data },
        });
      }
      return res
    },
    // 获取节点流程
    *getProgress({ payload }, { put, call }) {
      const res = yield call(getProgress, payload);
      yield put({
        type: 'updateProgress',
        payload: { progressList: res.data },
      });
    },
  },

  reducers: {
    // 更新List
    updateList(state, { payload: { list, total, pageCurrent, allPages } }) {
      return { ...state, list, total, pageCurrent, allPages };
    },
    // 更新Detail
    updateDetail(state, { payload: { detail } }) {
      return { ...state, detail };
    },
    // 更新Details
    updateDetails(state, { payload: { details } }) {
      return { ...state, details };
    },
    // 更新审批流程
    updateProgress(state, { payload }) {
      return { ...state, ...payload };
    },
    // 更细查询参数
    updateSerachParams(state, { payload: { serachParams } }) {
      return { ...state, serachParams }
    }
  },
};
